﻿using AnesSystem.JSClass;
using AnesSystem.Services;
using AnesSystem.Util;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// ZhiKongTongJi.xaml 的交互逻辑
    /// </summary>
    public partial class ZhiKongTongJiTableCtrl : UserControl
    {
        ExportToExcel excel = new ExportToExcel();
        JSZhiKongZhiBiao js = new JSZhiKongZhiBiao();

        public ZhiKongTongJiTableCtrl()
        {
            InitializeComponent();
            this.DataContext = ZhiKongTongJiCtrlViewMode.Instance;
            js.JSDoubleClicked += Js_JSDoubleClicked;
            webBrowser.Width = DataManager.Instance.CurrentControl.ActualWidth;
            webBrowser.Address = DataManager.Instance.CurrentHospital.WebUrl;
            webBrowser.RegisterJsObject("JSZhiKongZhiBiao", js);
            this.Loaded += ZhiKongTongJiTableCtrl_Loaded;
        }

        private void Js_JSDoubleClicked(object sender, JS2ClientArgs e)
        {
            DataManager.Instance.CurrentPatientLink = PatientLinkService.Instance.GetObjById(e.Id);
            Dispatcher.Invoke(delegate
            {
                MainPageCommonViewModel.Instance.NavigateTo("AnesRecordCtrl");
            });

        }

        private void Inquire_Click(object sender, RoutedEventArgs e)
        {
            SurefWeb();
        }

        public void SurefWeb()
        {

            string paramForZhikong = "{ 'starttime':'" + ZhiKongTongJiCtrlViewMode.Instance.StartTime.ToString(DateFormat.DATE_FORMAT_STR_YMD) + "','endtime':'" + ZhiKongTongJiCtrlViewMode.Instance.EndTime.ToString(DateFormat.DATE_FORMAT_STR_YMD) + "','name':'" + ZhiKongTongJiCtrlViewMode.Instance.AccountName + "'}";
            string text = System.Web.HttpUtility.UrlEncode(paramForZhikong, System.Text.Encoding.UTF8);
            webBrowser.Width = DataManager.Instance.CurrentControl.ActualWidth;
            webBrowser.Address = DataManager.Instance.CurrentHospital.WebUrl + "?data=" + text;
            Console.WriteLine("this url is :" + webBrowser.Address);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string command = (sender as Button).Tag.ToString();
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
                Task.Factory.StartNew(delegate
                {
                    HistoryCtrlViewModel.Instance.Excute(command);
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                });
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(GetType(), ex);
            }
        }

        /// 导出为Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExportParameterBtn_Click(object sender, RoutedEventArgs e)
        {
            SurefWeb();
            DateTime startTime = _startDate.SelectedDateTime;
            DateTime endTime = _endDate.SelectedDateTime;
            endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59);
            string accountName = _DoctorsName.Text;
            try
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
                Task.Factory.StartNew(delegate
                {
                    ExportToExcel.Instance.Excel.ExportParameterCommon(startTime.Year + "年 麻醉质量检测指标", startTime, endTime, accountName);
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                });
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(GetType(), ex);
            }

            //    ExportToExcel.Instance.Excel.QualityWorkExport();
            //ZhiKongTongJiCtrlViewMode.Instance.ZhiKongExcel(startTime, endTime, accountName);
        }

        private void QualityWorkExportBtn_Click(object sender, RoutedEventArgs e)
        {
            DateTime startTime = _startDate.SelectedDateTime;
            DateTime endTime = _endDate.SelectedDateTime;
            endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59);
            string accountName = _DoctorsName.Text;
            try
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
                Task.Factory.StartNew(delegate
                {
                    ZhiKongTongJiCtrlViewMode.Instance.GongZuoLiangExcel(startTime, endTime, accountName);
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                });
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(GetType(), ex);
            }
            //ExportToExcel.Instance.Excel.ExportParameterCommon(_startTime.SelectedDateTime.Year + "年 麻醉工作量统计", _endTime.SelectedDateTime, _endTime.SelectedDateTime, accountName);
        }
        //刷新
        private void ZhiKongTongJiTableCtrl_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}
